package com.hziee.graduate.ticket.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.graduate.ticket.dao.CinemaDao;
import com.hziee.graduate.ticket.dao.RegionDao;
import com.hziee.graduate.ticket.dao.UserDao;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.entity.Cinema;
import com.hziee.graduate.ticket.entity.Region;
import com.hziee.graduate.ticket.entity.User;
import com.hziee.graduate.ticket.service.UserService;
import com.hziee.graduate.ticket.utils.BeanConvertUtil;
import com.hziee.graduate.ticket.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@Service("userService")
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private CinemaDao cinemaDao;

    @Resource
    private RegionDao regionDao;

    @Override
    public R<UserVO> login(HttpSession session, String phone, String password) {
        // 根据手机号查找用户
        User userByPhone = userDao.selectOne(new QueryWrapper<User>().eq("phone", phone).eq("del_flag",0));
        // 将用户的密码与收到的密码进行比对
        if (userByPhone!=null && userByPhone.getPassword().equals(password)) {
            // 如果成功生成cookie并返回
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userByPhone,userVO);
            session.setAttribute("user", userByPhone);
            String token = RandomUtil.randomUUID();
            session.setAttribute("token", token);
            userVO.setToken(token);
            log.debug("用户：{} 登录成功", userByPhone.getNick());
            return R.success("登录成功",userVO);
        } else {
            // 如果失败则返回失败,用户未注册
            return R.error("用户未注册");
        }
    }

    @Override
    public R<List<UserVO>> getUserVO() {
        List<UserVO> users = userDao.getUserVO();
        return R.success(users);
    }

    @Override
    public R<UserVO> addCinemaManager(Map<String, Object> map) {
        Integer unitId = Integer.parseInt(map.get("id").toString());
        String phone = map.get("phone").toString();
        String nick = map.get("nick").toString();
        User user = new User();
        user.setNick(nick);
        user.setRole(2);
        user.setPassword("adminadmin");
        user.setPhone(phone);
        // 在region中找到该影院
        Cinema cinema = cinemaDao.selectOne(new QueryWrapper<Cinema>()
                .eq("self_id", unitId)
                .eq("del_flag", 0)
        );
        user.setUnitId(Integer.valueOf(cinema.getCinemaId().toString()));
        userDao.insert(user);
        return R.success();
    }

    @Override
    public R<UserVO> delCinemaManager(Map<String, Object> map) {
        String userId = map.get("userId").toString();
        User user = userDao.selectById(Long.parseLong(userId));
        user.setDelFlag(1);
        userDao.updateById(user);
        return R.success();
    }

    @Override
    public R<UserVO> editCinemaManager(Map<String, Object> map) {
        String userId = map.get("userId").toString();
        User user = userDao.selectById(Long.parseLong(userId));
        user.setNick(map.get("nick").toString());
        user.setPhone(map.get("phone").toString());
        userDao.updateById(user);
        return R.success();
    }

    @Override
    public R<Object> addBuyer(Map<String, Object> map) {
        // 获取信息
        String phone = map.get("phone").toString();
        String password = map.get("password").toString();
        // 查找手机用户是否存在
        User userByPhone = userDao.selectOne(new QueryWrapper<User>().eq("phone",phone));
        if(userByPhone != null){
            // 如果存在了，那么就不添加了
            return R.error("用户已存在");
        }else{
            // 否则添加用户
            User user = new User();
            user.setPhone(phone);
            user.setNick("buyer" + System.currentTimeMillis());
            user.setPassword(password);
            user.setRole(1);
            save(user);
        }
        return R.success();
    }

}

